#define _CRT_SECURE_NO_WARNINGS 1

#include"stack.h"

void STInit(ST* pst)

{

	assert(pst);

	pst->a = NULL;

	pst->top = 0;

	pst->capacity = 0;

}

void STDestroy(ST* pst)

{

	assert(pst);

	free(pst->a);

	pst->a = NULL;

	pst->top = 0;

	pst->capacity = 0;

}



void STPush(ST* pst, STDataType x)

{

	if (pst->top == pst->capacity)

	{

		int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;

		STDataType* tmp = realloc(pst->a, sizeof(int) * newcapacity);

		if (tmp == NULL)

		{

			perror("malloc fail");

			return;

		}

		pst->a = tmp;

		pst->capacity = newcapacity;

	}

	pst->a[pst->top] = x;

	pst->top++;

}



void STPop(ST* pst)

{

	assert(pst);

	assert(!STEmpty(pst));

	pst->top--;

}

STDataType STTop(ST* pst)

{

	assert(pst);

	assert(!STEmpty(pst));

	return pst->a[pst->top - 1];

}

bool STEmpty(ST* pst)

{

	assert(pst);

	return (pst->top == 0);

}

int STSize(ST* pst)

{

	assert(pst);

	return pst->top;

}






